Assignment: Join the global gender inequality index to spatial data of the World, creating a new column of difference in inequality between 2010 and 2019

First, let’s install and unpack some libraries

library(sf)
library(janitor)
library(tidyverse)
library(countrycode)
library(ggplot2)
library(tmap)
library(tmaptools)

Then, let’s read, clean and prepare the files

#Geometry of countries
w_countries <- st_read("World_Countries//World_Countries_Generalized.shp") %>%
  clean_names()
## Reading layer `World_Countries_Generalized' from data source 
##   `C:\Users\Tabata\Documents\CASA\CASA0005_GISS\GISS\Homework 04\World_Countries\World_Countries_Generalized.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 251 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -20037510 ymin: -30240970 xmax: 20037510 ymax: 18418390
## Projected CRS: WGS 84 / Pseudo-Mercator
head(w_countries,5)
## Simple feature collection with 5 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -19015950 ymin: -1617339 xmax: 8339582 ymax: 5260415
## Projected CRS: WGS 84 / Pseudo-Mercator
##          country iso    countryaff aff_iso                       geometry
## 1    Afghanistan  AF   Afghanistan      AF MULTIPOLYGON (((6821275 424...
## 2        Albania  AL       Albania      AL MULTIPOLYGON (((2178615 511...
## 3        Algeria  DZ       Algeria      DZ MULTIPOLYGON (((512443 4423...
## 4 American Samoa  AS United States      US MULTIPOLYGON (((-19007124 -...
## 5        Andorra  AD       Andorra      AD MULTIPOLYGON (((160949.7 52...
#Read de complete dataset from 1990 to 2021
data_complete <- read_csv("HDR25_Composite_indices_complete_time_series.csv", na = "NA") %>%
  clean_names()

#Select the gender inequality index (gii) coloumns
data_gii <- data_complete %>%
  select(contains("iso"),
         contains("country"),
         contains("gii_2010"), 
         contains("gii_2019")) %>%
  mutate(dif = gii_2010 - gii_2019)
head(data_gii)
## # A tibble: 6 × 5
##   iso3  country             gii_2010 gii_2019     dif
##   <chr> <chr>                  <dbl>    <dbl>   <dbl>
## 1 AFG   Afghanistan            0.704    0.676  0.0280
## 2 ALB   Albania                0.192    0.131  0.061 
## 3 DZA   Algeria                0.508    0.385  0.123 
## 4 AND   Andorra               NA       NA     NA     
## 5 AGO   Angola                 0.556    0.536  0.0200
## 6 ATG   Antigua and Barbuda   NA       NA     NA

Now we match the coloumns and merge the files

w_gii <- w_countries %>%
  mutate(iso3 = countrycode(iso, "iso2c", "iso3c"))%>%
  left_join(data_gii, by = c("iso3" = "iso3", "country" = "country"))%>%
  select("country", "iso3", "gii_2010", "gii_2019", "dif")

plot(w_gii["dif"], 
     pal = hcl.colors(9, "viridis"),
     main = "Diference in inequality index of the years 2010 and 2019")

DINAMIC MAP:

w_gii_4326 <- w_gii %>%
  st_transform(.,4326)
tmap_mode("view")
## ℹ tmap modes "plot" - "view"
## ℹ toggle with `tmap::ttm()`
tm_shape(w_gii_4326) +
  tm_polygons(fill="dif",
              fill.scale = tm_scale_intervals(values = "viridis", style = "jenks"),
              fill_alpha = 0.5,
              fill.legend = tm_legend(title = "Inequality index difference", size = 0.8)) +
              tm_basemap(server = "OpenStreetMap") +
                tm_compass(type = "arrow",
                           position = c("left", "bottom"),
                           size = 1) +
                tm_scalebar(position = c("left", "bottom")) +
                tm_title("Diference in inequality index of the years 2010 and 2019", 
                size = 2, 
                position = c("center", "top"))
gghist <- ggplot(w_gii, aes(x = dif)) +
  geom_histogram(bins = 30, color = "black", fill = "white") +
  labs(title = "Diference in inequality index of the years 2010 and 2019",
       x = "Difference",
       y = "Frecuency")
gghist + geom_vline(aes(xintercept = 0),
                        color = "blue",
                        linetype = "dashed",
                        size = 1) +
                      theme(plot.title = element_text(hjust = 0.5))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 110 rows containing non-finite outside the scale range
## (`stat_bin()`).

Since the difference was calculated as 2010 − 2019, the histogram shows that most countries (with positive values) reduced their inequality index.

Sources:

Global gender inequality data:

https://hdr.undp.org/data-center/documentation-and-downloads

Spatial data of the world:

https://hub.arcgis.com/datasets/2b93b06dc0dc4e809d3c8db5cb96ba69_0/explore?location=-2.688200%2C0.000000%2C1.41

Countrycode package:

Arel-Bundock, Vincent, Nils Enevoldsen, and CJ Yetman, (2018). countrycode: An R package to convert country names and country codes. Journal of Open Source Software, 3(28), 848, https://doi.org/10.21105/joss.00848